"""
While attempting to write a darts program in Objective C ... I thought ...
whqt I need is a tested Darts Server Engine thing to implement the game with
no particular output.

What more excuse do I need for a python design....

"""
from collections import deque

class Game(object):
    def __init__(self, server, callback):
        self.callback = callback
        self.server = server
        self.expected_team = None
        self.expected_player = None
        self.legs = 3
        self.sets = 1
        self.start_score = 501
        self.teams = deque()

    def _setter(self, attr, value):
        assert self.expected_team == self.expected_player == None
        uattr = '_%s' % attr
        if not hasattr(self, uattr) or getattr(self, uattr) != value:
            self.callback(attr, value)
        setattr(self, uattr, value)

    @property
    def legs(self):
        return self._legs
    @legs.setter
    def legs(self,N):
        self._setter('legs', N)
    @property
    def sets(self):
        return self._sets
    @sets.setter
    def sets(self,N):
        self._setter('sets', N)
    @property
    def start_score(self):
        return self._start_score
    @start_score.setter
    def start_score(self, N):
        self._setter('start_score', N)

    def add_team(self, name):
        result = Team(name)
        self.teams.append(result)
        return result
    def next_expected(self):
        self.teams.rotate()
        self.expected_team=self.teams[0]
        self.expected_team.players.rotate()
        self.expected_player=self.expected_team.players[0]
    def init_expected(self, team, player):
        assert team in self.teams
        self.expected_team = team
        assert player in team.players
        self.expected_player = player
        while team!=self.teams[0]:
            self.teams.rotate()
        while player!=team.players[0]:
            team.players.rotate()
    def score(self, team, player, score):
        if self.expected_team is None:
            self.init_expected(team, player)
        assert self.expected_team==team and self.expected_player==player
        self.next_expected()
    def abort(self):
        self.server.abort_game(self)

class Player(object):
    def __init__(self, name):
        self.name = name

class Team(object):
    def __init__(self, name):
        self.name = name
        self.players = deque()
    def add_player(self, name):
        result = Player(name)
        self.players.append(result)
        return result

class Server(object):
    def __init__(self):
        self.games = []
    def create_game(self, callback):
        result = Game(self, callback)
        self.games.append(result)
        return result
    def abort_game(self,game):
        pass


